<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.SQLException"%>
<%@ page import="java.sql.Statement"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.List"%>
<%@ page import="cn.edu.scau.cmi.luowanghaoyun.bean.StudentBean"%>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>显示学生列表</title>
<style type="text/css">td,th {	padding: 5px;}
</style>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");		
String url = "jdbc:mysql://localhost:3306/jsp";
String username = "root";						
String password = "root";						
Connection conn = DriverManager.getConnection(url,username,password);
Statement stmt = conn.createStatement();		
String sql = "select * from tb_student";		
ResultSet rs = stmt.executeQuery(sql);				
List<StudentBean> list = new ArrayList<>();		
while(rs.next()){								
	StudentBean student = new StudentBean();	
	student.setId(rs.getInt("id"));			
	student.setName(rs.getString("name"));		
	student.setSex(rs.getString("sex"));		
	student.setAge(rs.getInt("age"));	
	list.add(student); 							
}
request.setAttribute("list", list); 	



%>
	<div width="98%" align="center">
		<h2>所有学生信息</h2>
	</div>
	<table width="98%" border="0" align="center" cellpadding="0"
		cellspacing="1" bgcolor="#666666">
		<tr>
			<th bgcolor="#FFFFFF">id</th>
			<th bgcolor="#FFFFFF">姓名</th>
			<th bgcolor="#FFFFFF">性别</th>
			<th bgcolor="#FFFFFF">年龄</th>
			
		</tr>
		<%
			
			if (list == null || list.size() < 1) {
				out.print("<tr><td bgcolor='#FFFFFF' colspan='5'>没有任何学生信息！</td></tr>");
			} else {
				
				for (StudentBean student : list) {
		%>
		<tr align="center">
			<td bgcolor="#FFFFFF" ><%=student.getId()%></td>
			<td bgcolor="#FFFFFF"><%=student.getName()%></td>
			<td bgcolor="#FFFFFF"><%=student.getSex()%></td>
			<td bgcolor="#FFFFFF"><%=student.getAge()%></td>
		</tr>
		<%
			}
		}
		%>
	</table>
</body>
</html>